<?php
namespace app\sysadmin\controller;
use think\Db;
use Env;
class Index extends Common{
	public function __construct(){
		parent::__construct();
	}
    public function index(){
		$this->view->engine->layout(false);
		$menu_tree = $this->sys_menu_lists();
		$this->assign('menu_tree',$menu_tree);
		return $this->fetch();
    }
	// 清理缓存
	public function clear_cache(){
		\think\facade\Cache::clear();
		$path   = Env::get('runtime_path');
		\Wy\Dir::delDir($path.'');
		$this->success('清理成功');
	}

	// 退出登录
	public function unlogin(){
		session(null);
		$this->success('退出成功',url('login/index'));
	}

	// 后台左菜单
	private function sys_menu_lists(){
		$menu = cache('sysadmin_nav');
		if($menu){
			// return $menu;
		}
		$menu = Db::name('admin_nav')->where('pid','=','0')->where('status','=','1')->order('id asc')->select();
		if($menu){
			foreach($menu as $k=>$v){
				$menu[$k]['son_lists'] = Db::name('admin_nav')->where('pid','=',$v['id'])->where('status','=','1')->order('id asc')->select();
				if($menu[$k]['son_lists']){
					foreach($menu[$k]['son_lists'] as $a=>$b){
						$menu[$k]['son_lists'][$a]['son_lists'] = Db::name('admin_nav')->where('pid','=',$b['id'])->where('status','=','1')->order('id asc')->select();
						if(!$menu[$k]['son_lists'][$a]['son_lists']){
							unset($menu[$k]['son_lists'][$a]['son_lists']);
						}
					}
				}else{
					unset($menu[$k]);
				}
			}
		}
		cache('sysadmin_nav',$menu,600);
		return $menu;
	}
}
?>